package com.atguigu.reflect;

import java.lang.annotation.Annotation;

/**
 * Created by Smexy on 2024/4/1
 */
public class Demo15GetAnnotation {

    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {

        // 1、获取 Class 对象
        Class<Demo14UseMyAnnotation> demo14UseMyAnnotationClass = Demo14UseMyAnnotation.class;

        // 2、获取类上标记的注解
        Annotation[] classAnnotations = demo14UseMyAnnotationClass.getAnnotations();
        for (Annotation classAnnotation : classAnnotations) {
            System.out.println("classAnnotation = " + classAnnotation);
        }

        // 3、获取构造器上标记的注解
        Annotation[] constructorAnnotations = demo14UseMyAnnotationClass.getDeclaredConstructor().getAnnotations();
        for (Annotation constructorAnnotation : constructorAnnotations) {
            System.out.println("constructorAnnotation = " + constructorAnnotation);
        }

        // 4、获取成员变量上标记的注解
        Annotation[] fieldAnnotations = demo14UseMyAnnotationClass.getDeclaredField("myName").getAnnotations();
        for (Annotation fieldAnnotation : fieldAnnotations) {
            System.out.println("fieldAnnotation = " + fieldAnnotation);
        }

        // 5、获取方法上标记的注解
        Annotation[] methodAnnotations = demo14UseMyAnnotationClass.getDeclaredMethod("showMyName", String.class).getAnnotations();
        for (Annotation methodAnnotation : methodAnnotations) {
            System.out.println("methodAnnotation = " + methodAnnotation);
        }

        // 6、获取方法参数上标记的注解
        Annotation[][] paramAnnotations = demo14UseMyAnnotationClass.getDeclaredMethod("showMyName", String.class).getParameterAnnotations();
        for (Annotation[] paramAnnotation : paramAnnotations) {
            System.out.println("paramAnnotation = " + paramAnnotation);
        }
    }

}
